package com.coai.warehouse.service

import com.coai.warehouse.model.InboundItem
import com.coai.warehouse.model.InboundOrder
import com.coai.warehouse.model.OutboundOrder
import org.springframework.kafka.core.KafkaTemplate
import org.springframework.stereotype.Service

/**
 * 仓储事件服务 - 发布事件到Kafka用于数字孪生跟踪
 */
@Service
class WarehouseEventService(
    private val kafkaTemplate: KafkaTemplate<String, Any>
) {
    
    companion object {
        private const val TOPIC_WAREHOUSE_EVENTS = "warehouse-events"
    }
    
    /**
     * 发布入库单创建事件
     */
    fun publishInboundOrderCreated(order: InboundOrder) {
        val event = mapOf(
            "eventType" to "INBOUND_ORDER_CREATED",
            "orderId" to order.id,
            "orderNo" to order.orderNo,
            "warehouseId" to order.warehouseId,
            "supplierId" to order.supplierId,
            "type" to order.type.name,
            "plannedTime" to order.plannedTime.toString(),
            "timestamp" to System.currentTimeMillis()
        )
        
        kafkaTemplate.send(TOPIC_WAREHOUSE_EVENTS, order.id, event)
    }
    
    /**
     * 发布出库单创建事件
     */
    fun publishOutboundOrderCreated(order: OutboundOrder) {
        val event = mapOf(
            "eventType" to "OUTBOUND_ORDER_CREATED",
            "orderId" to order.id,
            "orderNo" to order.orderNo,
            "warehouseId" to order.warehouseId,
            "customerId" to order.customerId,
            "type" to order.type.name,
            "priority" to order.priority.name,
            "plannedTime" to order.plannedTime.toString(),
            "timestamp" to System.currentTimeMillis()
        )
        
        kafkaTemplate.send(TOPIC_WAREHOUSE_EVENTS, order.id, event)
    }
    
    /**
     * 发布质检完成事件
     */
    fun publishQualityCheckCompleted(item: InboundItem, result: QualityCheckResult) {
        val event = mapOf(
            "eventType" to "QUALITY_CHECK_COMPLETED",
            "itemId" to item.id,
            "orderId" to item.orderId,
            "productId" to item.productId,
            "qcStatus" to item.qcStatus?.name,
            "passed" to result.passed,
            "score" to result.score,
            "confidence" to result.confidence,
            "timestamp" to System.currentTimeMillis()
        )
        
        kafkaTemplate.send(TOPIC_WAREHOUSE_EVENTS, item.id, event)
    }
    
    /**
     * 发布拣货路径优化事件
     */
    fun publishPickingPathOptimized(orderId: String, result: PickingPathResult) {
        val event = mapOf(
            "eventType" to "PICKING_PATH_OPTIMIZED",
            "orderId" to orderId,
            "pathLength" to result.path.size,
            "estimatedDistance" to result.estimatedDistance,
            "estimatedTime" to result.estimatedTime,
            "timestamp" to System.currentTimeMillis()
        )
        
        kafkaTemplate.send(TOPIC_WAREHOUSE_EVENTS, orderId, event)
    }
    
    /**
     * 发布库位推荐事件
     */
    fun publishLocationRecommended(warehouseId: String, productId: String, locationId: String, score: Double) {
        val event = mapOf(
            "eventType" to "LOCATION_RECOMMENDED",
            "warehouseId" to warehouseId,
            "productId" to productId,
            "locationId" to locationId,
            "recommendScore" to score,
            "timestamp" to System.currentTimeMillis()
        )
        
        kafkaTemplate.send(TOPIC_WAREHOUSE_EVENTS, locationId, event)
    }
}
